package cn.liontalk.demo;

/**
 * @author: 周哲
 * @package: cn.liontalk.demo
 * @description: 对象锁示例1 代码块示例 手动指定锁对象
 * @date: 2019/1/26 12:06
 * @version: V1.0
 */
public class SynchronizedObjectCodeBlock2 implements Runnable {

    static SynchronizedObjectCodeBlock2 instance = new SynchronizedObjectCodeBlock2();
    static SynchronizedObjectCodeBlock2 instance2 = new SynchronizedObjectCodeBlock2();

    Object lock = new Object();
    Object lock2 = new Object();

    public void run() {


       /* synchronized (lock2) {
            System.out.println("我是对象锁的lock2 :  代码块形式：" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "：运行完成");
        }


        synchronized (lock) {
            System.out.println("我是对象锁的lock0 :  代码块形式：" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "：运行完成");
        }*/

        synchronized (this) {
            System.out.println("我是对象锁的lock0 :  代码块形式：" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "：运行完成");
        }


    }


    public static void main(String[] args) {

        Thread thread = new Thread(instance);
        Thread thread1 = new Thread(instance2);

        thread.start();
        thread1.start();

        while (thread.isAlive() || thread1.isAlive()) {

        }
        System.out.println("Finished");
    }
}
